package com.cxs.pasteboardtest.slice;

import com.cxs.pasteboardtest.ResourceTable;
import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.content.Intent;
import ohos.agp.components.*;
import ohos.data.usage.StatVfs;
import ohos.miscservices.pasteboard.IPasteDataChangedListener;
import ohos.miscservices.pasteboard.PasteData;
import ohos.miscservices.pasteboard.SystemPasteboard;

public class MainAbilitySlice extends AbilitySlice {

    SystemPasteboard pasteboard;

    @Override
    public void onStart(Intent intent) {
        super.onStart(intent);
        super.setUIContent(ResourceTable.Layout_ability_main);

        pasteboard = SystemPasteboard.getSystemPasteboard(this);

        findComponentById(ResourceTable.Id_btn_setpasteboard).setClickedListener(c -> addData());
        findComponentById(ResourceTable.Id_btn_getpasteboard1).setClickedListener(c -> getData1());
        findComponentById(ResourceTable.Id_btn_getpasteboard2).setClickedListener(c -> getData2());

        IPasteDataChangedListener listener = new IPasteDataChangedListener() {
            @Override
            public void onChanged() {
                PasteData pasteData = pasteboard.getPasteData();
                if (pasteData == null) {
                    return;
                }
                System.out.println(pasteData.getPrimaryText());
            }
        };
        pasteboard.addPasteDataChangedListener(listener);

    }

    private void getData2() {
        PasteData pasteData = pasteboard.getPasteData();
        if (pasteData == null) {
            return;
        }
        PasteData.DataProperty dataProperty = pasteData.getProperty();
        boolean hasHtml = dataProperty.hasMimeType(PasteData.MIMETYPE_TEXT_HTML);
        boolean hasText = dataProperty.hasMimeType(PasteData.MIMETYPE_TEXT_PLAIN);
        if (hasHtml || hasText) {
            Text text = (Text) findComponentById(ResourceTable.Id_text_helloworld);
            for (int i = 0; i < pasteData.getRecordCount(); i++) {
                PasteData.Record record = pasteData.getRecordAt(i);
                String mimeType = record.getMimeType();
                if (mimeType.equals(PasteData.MIMETYPE_TEXT_HTML)) {
                    text.setText(record.getHtmlText());
                    break;
                } else if (mimeType.equals(PasteData.MIMETYPE_TEXT_PLAIN)) {
                    text.setText(record.getPlainText().toString());
                    break;
                } else {
                    // skip records of other Mime type
                }
            }
        }
    }

    private void getData1() {
        PasteData pasteData = pasteboard.getPasteData();
        CharSequence primaryText = pasteData.getPrimaryText();
        System.out.println(primaryText);
    }

    private void addData() {
        if (pasteboard != null) {
            pasteboard.setPasteData(PasteData.creatPlainTextData("Hello, world!"));
        }
    }

    @Override
    public void onActive() {
        super.onActive();
    }

    @Override
    public void onForeground(Intent intent) {
        super.onForeground(intent);
    }
}
